<template>
  <a-modal :title="form.modelId ? '编辑' : '添加'" width="600px" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <a-row>
        <a-col :span="24">
          <a-form-item label="数据模型代码" name="modelCode">
            <a-input style="width: 100%" v-model:value="form.modelCode" placeholder="请输入数据模型代码" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="数据模型名称" name="modelName">
            <a-input style="width: 100%" v-model:value="form.modelName" placeholder="请输入数据模型名称" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="订阅主题" name="subscribeTopic">
            <a-input style="width: 100%" v-model:value="form.subscribeTopic" placeholder="请输入订阅主题" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="频率（秒）" name="frequency">
            <a-input style="width: 100%" v-model:value="form.frequency" placeholder="请输入频率（秒）" />
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24">
          <a-form-item label="是否启用" name="isEnable">
            <a-radio-group v-model:value="form.isEnable" name="radioGroup">
              <a-radio :value="true">是</a-radio>
              <a-radio :value="false">否</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col> -->
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>

<script setup>
  import { reactive, ref, nextTick, onMounted } from 'vue';
  import { defineProps } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { iotApi } from '/@/api/plat/dataModel';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { PageMixin } from '/@/utils/mixin';
  const { resetForm } = PageMixin();

  // ------------------------ 事件 ------------------------
  const emits = defineEmits(['reloadList']);
  const funList = ref([]);
  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    resetForm(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    modelCode: undefined,
    modelName: undefined,
    subscribeTopic: undefined,
    frequency: undefined,
    isEnable: false,
    status: undefined,
  };

  let form = reactive({ ...formDefault });

  const rules = {
    modelCode: [{ required: true, message: '请填写数据模型代码' }],
    modelName: [{ required: true, message: '请填写数据模型名称' }],
    frequency: [{ required: true, message: '请填写频率' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save(form);
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save(formData) {
    SmartLoading.show();
    try {
      if (formData.modelId) {
        await iotApi.update(formData);
      } else {
        formData.ts = +new Date();
        await iotApi.add(formData);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>

<style scoped>
  .ant-select {
    width: 100% !important;
  }
</style>
